from flask import current_app

import json

from app.models.bo.gaolu.gaolu_reason_input_bo import GaoLuReasonInputBO
from app.models.bo.gaolu.gaolu_reason_status_bo import GaoLuReasonStatusBO
from app.services.models.const import gaolu_rule_params, NORMAL_CODE, DATA_ERROR_CODE, LOW_CODE, HIGH_CODE
from app.services.models.pickle_model import PickleModel
import logging as log


class GaoLuReasonModel(PickleModel):
    def __init__(self):
        super().__init__(model_name="gaolu_reason_model")

    def process(self, gaolu_reason_input_bo: GaoLuReasonInputBO):
        try:
            # current_app.logger.info(json.dumps(gaolu_reason_input_bo.get_value_dict()))
            gaolu_reason_status_bo = GaoLuReasonStatusBO()
            for param in gaolu_rule_params:
                gaolu_reason_status_bo.__setattr__(param + '_status',
                                                   get_param_status(gaolu_reason_input_bo.CG_LT_GL_GL04_RLB,
                                                                    gaolu_reason_input_bo.CG_LT_GL_GL04_FLiLunTie,
                                                                    gaolu_reason_input_bo.__getattribute__(param),
                                                                    param,
                                                                    self.model_instance[param]))
            return gaolu_reason_status_bo
        except Exception as e:
            current_app.logger.error(e, exc_info=1)

    def get_reason_scope(self, gaolu_reason_input_bo: GaoLuReasonInputBO):

        return {param+"_status": get_param_scope(gaolu_reason_input_bo.CG_LT_GL_GL04_RLB,
                                       gaolu_reason_input_bo.CG_LT_GL_GL04_FLiLunTie,
                                       gaolu_reason_input_bo.__getattribute__(param),
                                       param,
                                       self.model_instance[param]) for param in gaolu_rule_params}


def get_param_status(RLB, LLT, param, param_name, rule_dict):
    try:
        p = None
        for key in rule_dict.keys():
            if RLB in key[0] and LLT in key[1]:
                p = rule_dict[key]
        if p is None:
            return DATA_ERROR_CODE
        if param is None:
            return DATA_ERROR_CODE
        if param in p:
            return NORMAL_CODE
        elif param < p:
            return LOW_CODE
        elif param > p:
            return HIGH_CODE
    except Exception as e:
        current_app.logger.error("param " + param_name + " cause exception.", exc_info=1)
        return DATA_ERROR_CODE


def get_param_scope(RLB, LLT, param, param_name, rule_dict):
    try:
        p = None
        for key in rule_dict.keys():
            if RLB in key[0] and LLT in key[1]:
                p = rule_dict[key]
                lower = p.lower
                upper = p.upper
                return "[{},{}]".format(round(lower, 3), round(upper, 3))

    except Exception as e:
        current_app.logger.error("param " + param_name + " cause exception.", exc_info=1)
        return ""


# 用导出module的方法创建单例
gaolu_reason_model = GaoLuReasonModel()
